package net.neptech.node;

import lombok.extern.slf4j.Slf4j;
import net.neptech.model.dto.NodeDataDto;
import net.neptech.model.dto.node.Node;
import net.neptech.service.NodeDataService;
import net.neptech.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * @Author Saya
 * @Date 2025/8/19 15:46
 * @Description TODO
 **/
@Component
@Slf4j
@Lazy
public class DataStoreHandler {
    @Autowired
    private NodeDataService nodeDataService;

    /**
     * 节点数据存储
     *
     * @param flowId 流程id
     * @param nodeId 节点id
     * @param data   数据
     */

    public void saveAll(String flowId, String nodeId, Object data) {
        NodeDataDto processNodeDataDto = new NodeDataDto();
        processNodeDataDto.setFlowId(flowId);
        processNodeDataDto.setNodeId(nodeId);
        processNodeDataDto.setData(JsonUtil.toJSONString(data));
        nodeDataService.saveNodeData(processNodeDataDto);
    }

    /**
     * 节点数据存储
     *
     * @param flowId 流程id
     * @param nodeId 节点id
     * @param data   数据
     */

    public void save(String flowId, String nodeId, Node data) {
        log.debug("flowId={} nodeId={} data={}", flowId, nodeId, data);
        saveAll(flowId, nodeId, data);
    }
    /**
     * 获取节点数据
     *
     * @param flowId 流程id
     * @param nodeId 节点id
     * @return
     */
    public String get(String flowId, String nodeId) {
        return nodeDataService.getNodeData(flowId, nodeId);
    }


    public Node getNode(String flowId, String nodeId) {
        String text = get(flowId, nodeId);
        return JsonUtil.parseObject(text, Node.class);
    }
}
